package com.example.gokuplayalong.service.privateclient.service.serviceImpl;

import com.alibaba.fastjson.JSONObject;
import com.example.gokuplayalong.bases.base.ExceptionHandler;
import com.example.gokuplayalong.bases.base.Tools;
import com.example.gokuplayalong.bases.mysql.MysqlDruid;
import com.example.gokuplayalong.bases.security.Communication;
import com.example.gokuplayalong.service.privateclient.service.PrivateClientService;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

@Repository("PrivateClientService")
public class PrivateClientServiceImpl implements PrivateClientService {
    @Override
    public String encrypt(String msg) {
        Communication communication = new Communication();
        return communication.MessageEncrypt(msg);
    }

    @Override
    public String decrypt(String msg) {
        Communication communication = new Communication();
        return communication.MessageDecrypt(msg);
    }

    @Override
    public String login(String msg) {
        Connection connection = null;
        PreparedStatement psmt;
        ResultSet rs;
        String token = "";
        String nickname = "";
        String userid = "";
        JSONObject msgToClient = new JSONObject();
        try {
            connection = MysqlDruid.dataSource.getConnection();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
        }
        msgToClient.put("result", "失败");
        System.out.println(msg);
        try {
            JSONObject bean = JSONObject.parseObject(msg);
            if (bean != null) {
                psmt = connection.prepareStatement("select * from profileinfo where phone=\"" + bean.getString("phone") + "\"");
                rs = psmt.executeQuery();
                if (rs.next()) {
                    connection.setAutoCommit(false);
                    token = Tools.getOrderNum();
                    psmt = connection.prepareStatement("update profileinfo set token=\"" + token + "\" where phone=\"" + bean.getString("phone") + "\"");
                    psmt.executeUpdate();
                    connection.commit();
                    msgToClient.put("result", "成功");

                }
                else {
                    connection.setAutoCommit(false);
                    token = Tools.getOrderNum();
                    userid = "3" + Tools.getOrderNum();
                    nickname = "新用户" + Tools.getInitRandomUserId("3", 6);
                    psmt = connection.prepareStatement("insert into profileinfo(nickname,userid,token,phone,registertime) values(\"" + nickname + "\",\"" + userid + "\",\"" + token + "\",\"" + bean.getString("phone") + "\",\"" + Tools.getSDF().format(System.currentTimeMillis()) + "\")");
                    psmt.executeUpdate();
                    connection.commit();
                    msgToClient.put("result", "成功");
                }
                psmt = connection.prepareStatement("select * from profileinfo where phone=\"" + bean.getString("phone") + "\"");
                rs = psmt.executeQuery();
                if (rs.next()) {
                    msgToClient.put("params", new JSONObject(Tools.getResultMap(rs)));
                }
            }

        } catch (Throwable e) {
            try {
                connection.rollback();
                if (!connection.isClosed()) connection.close();

            } catch (Throwable ex) {
                ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), ex);

            }
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
        }
        System.out.println(msgToClient.toJSONString());
        try {
            if (connection != null && !connection.isClosed()) connection.close();
        } catch (Throwable e) {
            ExceptionHandler.saveException(Thread.currentThread().getStackTrace()[1].getClassName() + Thread.currentThread().getStackTrace()[1].getMethodName() + Thread.currentThread().getStackTrace()[1].getLineNumber(), e);
        }
        return encrypt(msgToClient.toJSONString());
    }

}
